﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using FTPclient;
namespace FTPclient
{
    public partial class Form1 : Form
    {
        private static FTPHelper ftpHelper;
        public Form1()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void splitContainer2_Panel2_Paint(object sender, PaintEventArgs e)
        {
               
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "开始下载文件:" + textBox6.Text + "至" + textBox5.Text + "中\n";
            string newPath = textBox5.Text + "\\" + listBox2.SelectedItem.ToString();
            if (!File.Exists(textBox5.Text))
            {
                File.Create(newPath).Dispose();
                ftpHelper.download(textBox6.Text, newPath);
            }
            else
            {
                ftpHelper.download(textBox6.Text, textBox5.Text);
            }
            richTextBox1.Text += "下载成功！\n";


        }

        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "开始上传文件" + textBox5.Text + "至" + textBox6.Text + "中\n";
            //string newPath = textBox6 + "/" + listBox2.SelectedItem.ToString();
            if (listBox2.SelectedItem == null)
            {
                ftpHelper.upload(textBox5.Text, textBox6.Text);
            }
            else
            {
                ftpHelper.upload(textBox5.Text, textBox6.Text + "/" + listBox2.SelectedItem.ToString());
            }
            richTextBox1.Text += "上传成功！\n";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //ftpHelper = new FTPHelper(textBox1.Text, int.Parse(textBox4.Text), textBox2.Text, textBox3.Text);
            ftpHelper = new FTPHelper("127.0.0.1", 21, "root", "12345678");
            this.richTextBox1.Text += "正在与" + textBox1.Text + "进行连接\n";
            string[] files = ftpHelper.get_files("/");
            string[] directories = ftpHelper.get_directories("/");
            if (files[0] == "error" && directories[0] == "error")
            {
                this.richTextBox1.Text += "连接失败！\n";
            }
            else
            {
                this.richTextBox1.Text += "连接成功！\n";
                for(int i = 1; i < files.Length; i++)
                {
                    string file = files[i - 1];
                    this.treeView3.Nodes.Add(file);
                }
                for(int i = 1; i < directories.Length; i++)
                {
                    string directory = directories[i - 1];
                    this.treeView3.Nodes.Add(directory);
                }
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            textBox5.Text = e.Node.Text;
            if (e.Node.Nodes.Count > 0)
            {
                if (e.Node.IsExpanded)
                {
                    e.Node.Collapse();

                }
                else
                {
                    e.Node.Expand();
                }
            }
            else
            {
                if (Directory.Exists(e.Node.Name))
                {
                    try
                    {
                        string[] allDirectory = Directory.GetDirectories(e.Node.Name);
                        foreach (string s in allDirectory)
                        {
                            e.Node.Nodes.Add(s, s.Remove(0, s.LastIndexOf("//") + 1));
                        }
                        listBox1.Items.Clear();
                        string[] allFiles = Directory.GetFiles(e.Node.Name);
                        foreach (string sf in allFiles)
                        {
                            listBox1.Items.Add(sf.Remove(0, sf.LastIndexOf("//") + 1));
                        }
                    }
                    catch
                    {
                    }
                }
                e.Node.Expand();
            }
        }

        private void treeView3_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            textBox6.Text = e.Node.Text;
            //if (Directory.Exists(e.Node.Name))
            //{

            string input = "/" + e.Node.Text;
            TreeNode read = e.Node;
            while (read.Parent != null)
            {
                read = read.Parent;
                input = "/" + read.Text + input;
            }
            textBox6.Text = input;
            string[] allDirectory = ftpHelper.get_directories(input);
            string[] files = ftpHelper.get_files(input);
            if (e.Node.Nodes.Count > 0)
            {
                if (e.Node.IsExpanded)
                {
                    e.Node.Collapse();

                }
                else
                {
                    e.Node.Expand();
                }
            }
            else
            {
                if (allDirectory != null)
                {
                    for (int i = 1; i < allDirectory.Length; i++)
                    {
                        string directory = allDirectory[i - 1];
                        if (directory != "error" && directory != "\n" && directory != e.Node.Text)
                        {
                            e.Node.Nodes.Add(directory);
                        }
                    }
                }
            }
            listBox2.Items.Clear();
            if (files != null)
            {
                for (int i = 1; i < files.Length; i++)
                {
                    string file = files[i - 1];
                    if (file != "error" && file != "\n")
                    {
                        listBox2.Items.Add(file);
                    }
                }
                e.Node.Expand();
            }
            //}
        }
        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void splitContainer2_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox5.Text = listBox1.SelectedItem.ToString();
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //textBox6.Text =  listBox2.SelectedItem.ToString();
        }
        private void splitContainer6_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }
    }
}
